package com.example.fashion.android.NewWorkCommunication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.fashion.android.R;
import com.example.fashion.android.entity.CharInfo;

import java.text.SimpleDateFormat;

/**
 * Created by fashion on 2017/7/14.
 * 聊天室的适配器
 */
public class CharAdapter extends RecyclerView.Adapter {
    private SparseArray<CharInfo> mCharInfoArray;
    private Context mContext;

    public CharAdapter(Context context, SparseArray<CharInfo> charInfoArray) {
        this.mContext = context;
        this.mCharInfoArray = charInfoArray;
    }

    @Override
    public int getItemCount() {
        return mCharInfoArray.size();
    }

    @Override
    public int getItemViewType(int position) {
        //返回消息类型(0:收到的消息 1:回复的消息)
        return mCharInfoArray.get(position).getSendType() == 0 ? 0 : 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {      //收到的消息
            View receiver = View.inflate(mContext, R.layout.item_receiver,null);
            return new CharViewHolder(receiver);
        } else {      //回复的消息
            View reply = View.inflate(mContext, R.layout.item_reply,null);
            return new CharViewHolder(reply);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((CharViewHolder)holder).userIcon.setImageResource(R.mipmap.ic_launcher);
        ((CharViewHolder)holder).userInfo.setText(mCharInfoArray.get(position).getCharScript());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ((CharViewHolder)holder).userTime.setText(formatter.format(mCharInfoArray.get(position).getTime()));
    }

    class CharViewHolder extends RecyclerView.ViewHolder {
        private ImageView userIcon;
        private TextView userInfo;
        private TextView userTime;

        public CharViewHolder(View itemView) {
            super(itemView);
            userIcon = (ImageView) itemView.findViewById(R.id.iv_user_icon);
            userInfo = (TextView) itemView.findViewById(R.id.tv_user_info);
            userTime = (TextView) itemView.findViewById(R.id.tv_user_time);
        }
    }


}
